// 引用命名规范
import * as actionTypes from './constants';
// 引用全局接口
import {IDefaultState} from '../global/globalInterFace'
// TS规范
declare interface IAction {
    type: string,
    data: any,
    index: number
}
// 渲染的数据
let tempData  = [];
for (let i = 0; i < 5; i++) {
    tempData.push({
        key: i,
        Number: i,
        Name: `Edward King ${i}`,
        Moderators: [4000, 4001],
    })
}



const defaultState: IDefaultState= {
    dataSource: tempData,
};

export default (state: IDefaultState = defaultState, action: IAction) => {
    let tempState;
    switch (action.type) {
        case actionTypes.DELETE_ITEM:
            tempState = JSON.parse(JSON.stringify(state));
            tempState.dataSource.splice(action.data, 1);
            return tempState;
        case actionTypes.DELETE_BATCH:
            tempState = JSON.parse(JSON.stringify(state));
            tempState.dataSource.splice(action.data, action.data.length);
            return tempState;
        case actionTypes.ADD_ITEM:
            tempState = JSON.parse(JSON.stringify(state));
            tempState.dataSource.push(action.data);
            return tempState;
        case actionTypes.EDIT_ITEM:
            tempState = JSON.parse(JSON.stringify(state));
            tempState.dataSource[action.index] = action.data;
            return tempState;
        default:
            return state;
    }
}